package chapter

import scala.slick.driver.H2Driver
import scala.slick.driver.H2Driver.simple._

object CoffeeApp extends App {
  // Definition of the COFFEES table
  case class Coffee(name: String, supID: Int, price: Double, sales: Int, total: Int)
  case class Coffees(tag: Tag) extends Table[Coffee](tag, "COFFEES") {
    def name = column[String]("COF_NAME", O.PrimaryKey)
    def supID = column[Int]("SUP_ID")
    def price = column[Double]("PRICE")
    def sales = column[Int]("SALES")
    def total = column[Int]("TOTAL")
    def * = (name, supID, price, sales, total) <> (Coffee.tupled, Coffee.unapply _)
  }
  val coffees = TableQuery[Coffees]
  Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver") withSession {
    implicit session =>
      coffees.ddl.create

      coffees ++= Seq(
        Coffee("Colombian", 101, 7.99, 0, 0),
        Coffee("French_Roast", 49, 8.99, 0, 0),
        Coffee("Espresso", 150, 9.99, 0, 0),
        Coffee("Colombian_Decaf", 101, 8.99, 0, 0),
        Coffee("French_Roast_Decaf", 49, 9.99, 0, 0))

      println("Update -=-=-=--=-=-=-=-=-=")
      coffees.where(_.name === "Colombian").update(Coffee("Colombian", 101, 7.99, 101, 101))

      // Iterate through all coffees and output them
      println("Coffees:")
      coffees foreach {
        case Coffee(name, supID, price, sales, total) =>
          println("  " + name + "\t" + supID + "\t" + price + "\t" + sales + "\t" + total)
      }

      val once = coffees.filter(_.name === "ddd").firstOption

      
      
      
      println(once)

  }
}